package com.example.novelweb.controller;

import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.exception.BusinessException;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.dto.chapter.ChapterAddRequest;
import com.example.novelweb.model.dto.chapter.ChapterSaveRequest;
import com.example.novelweb.model.dto.chapter.ChapterSwitchRequest;
import com.example.novelweb.model.entity.Chapter;
import com.example.novelweb.model.vo.ChapterInfo;
import com.example.novelweb.model.vo.ChapterVO;
import com.example.novelweb.service.ChapterService;
import io.swagger.annotations.ApiImplicitParam;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/chapter")
public class ChapterController {

  @Resource
  private ChapterService chapterService;


  @GetMapping("/list")
  public BaseResponse<List<ChapterVO>> listNovelAllChapter(Long novelId, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIfCheckIdsAny(novelId);
    return chapterService.listAllByNovelId(novelId);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @GetMapping("/my-novel")
  public BaseResponse<List<Chapter>> listMyNovelChapter(Long novelId,HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIfCheckIdsAny(novelId);
    return chapterService.listMyNovel(novelId);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = false, dataType = "String",paramType="header")
  @GetMapping("/get-by-id")
  public BaseResponse<Chapter> getChapterById(Long chapterId,HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(chapterId ==null || chapterId<=0, ErrorCode.PARAMS_ERROR);

    return chapterService.getChapterById(chapterId);
  }


  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/create")
  public BaseResponse<Long> createChapter(@RequestBody ChapterAddRequest chapterAddRequest,HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(chapterAddRequest ==null, ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIfCheckIdsAny(chapterAddRequest.getNovelId());
    if(StringUtils.isAnyBlank(chapterAddRequest.getTitle()
        ,chapterAddRequest.getTxt())) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    return chapterService.create(chapterAddRequest);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @GetMapping("/list-reviewing")
  public BaseResponse<List<ChapterInfo>> listReviewingChapter(HttpServletRequest httpServletRequest) {
    return chapterService.listReviewingChapter();
  }


  /**
   * save : update chapter.txt and chapter.title
   * */
  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/save")
  public BaseResponse<Boolean> saveChapter(@RequestBody ChapterSaveRequest chapterSaveRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(chapterSaveRequest == null ,ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIfCheckIdsAny(chapterSaveRequest.getId());

    if(StringUtils.isAnyBlank(chapterSaveRequest.getTitle()
        , chapterSaveRequest.getTxt())) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    return chapterService.save(chapterSaveRequest);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/switch")
  public BaseResponse<Boolean> switchChapterStatus(@RequestBody ChapterSwitchRequest chapterSwitchRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(chapterSwitchRequest == null ,ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIfCheckIdsAny(chapterSwitchRequest.getId());

    Integer status = chapterSwitchRequest.getStatus();
    ThrowUtils.throwIf(status ==null || status <0 ,ErrorCode.PARAMS_ERROR);


    return chapterService.switchStatus(chapterSwitchRequest);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/del")
  public BaseResponse<Boolean> delChapter(@RequestParam("chapterId") Long chapterId, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIfCheckIdsAny(chapterId);

    return chapterService.deleteChapter(chapterId);
  }




}
